﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
    public class RoleService
    {
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetList()
        {
            string sql = "select *,(select Name from dbo.TB_Admin where id=TB_Roles.UserId ) as UserName from TB_Roles where del=0";
            return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Edit(TB_Roles entity)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Id",SqlDbType.BigInt),
                 new SqlParameter("@RoleName",SqlDbType.VarChar,50),
                  new SqlParameter("@IsExit",SqlDbType.VarChar,150),
                   new SqlParameter("@Remark",SqlDbType.VarChar,150),
                         new SqlParameter("@Date",SqlDbType.DateTime),
                         new SqlParameter("@UserId",SqlDbType.Int), 
            };
            parameters[0].Value = entity.Id;
            parameters[1].Value = entity.RoleName;
            parameters[2].Value = entity.IsExit;
            parameters[3].Value = entity.Remark;
            parameters[4].Value = entity.Date;
            parameters[5].Value = entity.UserId;
            string sql = entity.Id == 0 ?
                "insert into dbo.TB_Roles(RoleName,Del,IsExit,UserId,Date,Remark)values(@RoleName,0,@IsExit,@UserId,@Date,@Remark)"
                : "update dbo.TB_Roles set RoleName=@RoleName,IsExit=@IsExit,Remark=@Remark where id=@Id";
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Exit(int id)
        {
            string sql = "update TB_Roles set isexit=1 where id="+id;
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql);
        }

        /// <summary>
        /// 权限分配
        /// </summary>
        /// <param name="rid"></param>
        /// <param name="menuids"></param>
        /// <returns></returns>
        public int FpRoles(int rid, string menuids)
        {
            string sql = "begin transaction " +
                         " delete TB_UserRole where roleid=" + rid;
            var ids = menuids.Split(',');
            foreach (string i in ids)
            {
                sql += " insert into TB_UserRole (MenuId,Roleid,del)values('"+i+"','"+rid+"',0) ";
            }
            sql += " commit transaction";
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql);
        }

       /// <summary>
       /// 获取所有角色
       /// </summary>
       /// <returns></returns>
        public DataTable GetAllList()
        {
            string sql = "select Id,RoleName from TB_Roles where del=0";
            return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
        }

    }
}
